package zozopo.warehouse.flow;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;


import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.flow.comm.Flow;
import zozopo.flow.operations.FlowOS;
import zozopo.flow.operations.FlowTB;
import zozopo.flow.operations.RecordlistOS;
import zozopo.flow.operations.RecordlistTB;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptTB;
import zozopo.warehouse.operations.ReceiptlistOS;
import zozopo.warehouse.operations.ReceiptlistTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ConfirmShipPriceFlowAction extends ActionSupport implements Action,SessionAware{
	
	private static final String limitedCode ="FLOW.WAREHOUSE.RECEIPTLIST.CONFIRMSHIPPRICE";

	//传入参数
		
	private String kind = "";
	private int kindid ;
	private String remark = "" ;
	private String flowCode = "";
	private double doublevalue_recordlist;
	
	//分页参数
	private int beginPageNum = 0;
	private int pageSize = 20;
	private int rowNum = 0;
	//传出参数
	//传出参数
	private List<FlowTB> flowList;
	private Map<Integer,List<FlowTB>> flowValuelistMap;
	private Map<Integer,RecordlistTB> recorlistMap;
	private Map<String,RecordlistTB> flowValuelistMapNoCancel;
	
	private Map<String,FlowTB> flowCodeMap;
	
	//显示出仓单数据
	private ReceiptlistTB receiptlist;
	private List<ReceiptTB> receiptList;
	private String state = "";
	private String lastSalesDate = "";
	
	
	private int flowValue;
	private int flowNextValue;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		///权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operator == null){
			addFieldError("errormessage", "用户未登陆或超时.请登陆后操作.");
			return NONE;
		}
		
		Flow flow = new Flow();
		
		ReceiptlistOS receiptlistOS = new ReceiptlistOS(flow.getConn());
		
		ReceiptOS receiptOS = new ReceiptOS(flow.getConn());
		
		
		//如果是出仓单流程 取得出仓信息
		if(kind.equals("FLOW.WAREHOUSE.RECEIPTLIST")){
			receiptlist = receiptlistOS.getReceiptlistById(kindid);
			receiptList = receiptOS.getListByReceiptlistId(kindid);
		}
		
		RecordlistTB recordlist = flow.getRecordlist(kind, kindid);
		if(!flowCode.equals("")){
			
			flow.executeFlow(kind, kindid, flowCode, operator, operatorlimitedMap,remark,doublevalue_recordlist);
			recordlist = flow.getRecordlist(kind, kindid);
		}
		
		recorlistMap = flow.getRecordlistIdMap(kind, kindid);
		flowValue = recordlist.getValue_recordlist();
		flowNextValue = recordlist.getNextvalue_recordlist();
		flowList = flow.getFlowListByKind(kind);
		flowCodeMap = flow.getFlowCodeMapByKind(kind);
		flowValuelistMap = flow.getFlowValueMapByKind(kind);
		flowValuelistMapNoCancel = flow.getRecordlistCodeMapNoCancel(kind, kindid);
		
		//flow.getFlowValueMapByKind(kind);
		receiptlistOS.upadteForProfit(receiptlistOS.getReceiptlistById(kindid),doublevalue_recordlist);
		
		
		//如果是出仓单流程 取得出仓信息
		if(kind.equals("FLOW.WAREHOUSE.RECEIPTLIST")){
			receiptlist = receiptlistOS.getReceiptlistById(kindid);
			receiptList = receiptOS.getListByReceiptlistId(kindid);
			
			ReceiptlistTB lastReceiptlist = receiptlistOS.getLastReceiptlistByCustomerid(receiptlist.getCustomerid_receiptlist());


			if(lastReceiptlist == null){
				lastSalesDate = "新用户!";
			}else{
				lastSalesDate ="上次购买时间:" + lastReceiptlist.getDate_receiptlist() + "";
			}
		}
		//rowNum = flow.getRowNum();
		flow.releaseConnection(); 
		//权限处理
		return SUCCESS;
	}
	
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户没有查看进货权限.");
			return;
		}
	}
	
	public int getBeginPageNum() {
		return beginPageNum;
	}
	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowNum() {
		return rowNum;
	}
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}
	public List<FlowTB> getFlowList() {
		return flowList;
	}
	public void setFlowList(List<FlowTB> flowList) {
		this.flowList = flowList;
	}

	public int getKindid() {
		return kindid;
	}
	public void setKindid(int kindid) {
		this.kindid = kindid;
	}
	public Map<Integer, List<FlowTB>> getFlowValuelistMap() {
		return flowValuelistMap;
	}
	public void setFlowValuelistMap(Map<Integer, List<FlowTB>> flowValuelistMap) {
		this.flowValuelistMap = flowValuelistMap;
	}
	public String getKind() {
		return kind;
	}
	public void setKind(String kind) {
		this.kind = kind;
	}
	public int getFlowValue() {
		return flowValue;
	}
	public void setFlowValue(int flowValue) {
		this.flowValue = flowValue;
	}
	public int getFlowNextValue() {
		return flowNextValue;
	}
	public void setFlowNextValue(int flowNextValue) {
		this.flowNextValue = flowNextValue;
	}

	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getFlowCode() {
		return flowCode;
	}
	public void setFlowCode(String flowCode) {
		this.flowCode = flowCode;
	}
	public double getDoublevalue_recordlist() {
		return doublevalue_recordlist;
	}
	public void setDoublevalue_recordlist(double doublevalue_recordlist) {
		this.doublevalue_recordlist = doublevalue_recordlist;
	}
	public Map<String, RecordlistTB> getFlowValuelistMapNoCancel() {
		return flowValuelistMapNoCancel;
	}
	public void setFlowValuelistMapNoCancel(
			Map<String, RecordlistTB> flowValuelistMapNoCancel) {
		this.flowValuelistMapNoCancel = flowValuelistMapNoCancel;
	}
	public Map<String, FlowTB> getFlowCodeMap() {
		return flowCodeMap;
	}
	public void setFlowCodeMap(Map<String, FlowTB> flowCodeMap) {
		this.flowCodeMap = flowCodeMap;
	}
	public void setRecorlistMap(Map<Integer, RecordlistTB> recorlistMap) {
		this.recorlistMap = recorlistMap;
	}
	public Map<Integer, RecordlistTB> getRecorlistMap() {
		return recorlistMap;
	}
	public ReceiptlistTB getReceiptlist() {
		return receiptlist;
	}
	public void setReceiptlist(ReceiptlistTB receiptlist) {
		this.receiptlist = receiptlist;
	}
	public List<ReceiptTB> getReceiptList() {
		return receiptList;
	}
	public void setReceiptList(List<ReceiptTB> receiptList) {
		this.receiptList = receiptList;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getLastSalesDate() {
		return lastSalesDate;
	}
	public void setLastSalesDate(String lastSalesDate) {
		this.lastSalesDate = lastSalesDate;
	}
	public static String getLimitedcode() {
		return limitedCode;
	}


}
